import request from '@/utils/request'

/**
 * 自定义文章管理API
 */

// 创建自定义文章
export function createCustomArticle(data) {
  const formData = new FormData()
  
  // 添加文本字段
  formData.append('title', data.title)
  formData.append('description', data.description)
  
  // 处理content字段 - 如果是字符串则直接使用，否则JSON序列化
  if (typeof data.content === 'string') {
    formData.append('content', data.content)
  } else {
    formData.append('content', JSON.stringify(data.content))
  }
  
  formData.append('status', data.status || 'draft')
  formData.append('is_featured', data.is_featured || false)
  formData.append('is_active', data.is_active !== false)
  formData.append('sort', data.sort || 1)
  formData.append('show_type', data.show_type || 1)
  if (data.show_start) {
    formData.append('show_start', data.show_start)
  }
  if (data.show_end) {
    formData.append('show_end', data.show_end)
  }
  
  // 添加文件ID字段
  console.log(data.background_music)
  console.log(data.background_image)
  if (data.background_music) {
    formData.append('background_music', JSON.parse(data.background_music).file_id)
  }
  if (data.background_image) {
    formData.append('background_animation', JSON.parse(data.background_image).file_id)
  }
  
  return request({
    url: '/admin/articles/custom',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 获取文章详情
export function getArticleDetail(articleId) {
  return request({
    url: `/api/admin/articles/${articleId}`,
    method: 'post'
  })
}

// 更新文章
export function updateArticle(articleId, data) {
  return request({
    url: `/api/admin/articles/update/${articleId}`,
    method: 'post',
    data
  })
}

// 发布文章
export function publishArticle(articleId) {
  return request({
    url: `/api/admin/articles/${articleId}/publish`,
    method: 'post',
    data: {
      articleId
    }
  })
}

// 删除文章
export function deleteArticle(articleId) {
  return request({
    url: `/api/admin/articles/delete/${articleId}`,
    method: 'post',
    data: {
      articleId
    }
  })
}

// 批量操作文章
export function batchActionArticles(articleIds, action) {
  return request({
    url: '/api/admin/articles/batch-action',
    method: 'post',
    data: {
      article_ids: articleIds,
      action
    }
  })
}

// 上传文章图片
export function uploadArticleImage(file) {
  const formData = new FormData()
  formData.append('image', file)
  
  return request({
    url: '/api/admin/articles/upload-image',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 文章状态选项
export const ARTICLE_STATUS = {
  DRAFT: 'draft',
  PUBLISHED: 'published',
  ARCHIVED: 'archived'
}

// 文章内容类型
export const CONTENT_TYPES = {
  TEXT: 'text',
  IMAGE: 'image',
  VIDEO: 'video',
  AUDIO: 'audio'
}

// 导出API对象
export const customArticleApi = {
  create: createCustomArticle,
  getDetail: getArticleDetail,
  update: updateArticle,
  publish: publishArticle,
  delete: deleteArticle,
  batchAction: batchActionArticles,
  uploadImage: uploadArticleImage
}